package com.welsy.authoring.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.welsy.authoring.business.*;
	import com.welsy.authoring.event.AddCourseEvent;
	import com.welsy.authoring.model.CourseModelLocator;
	import com.welsy.authoring.vo.*;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;	
	
	/**
	  * The command for course retrieval
	  * @author Welsy.org
 	  * @version	$Revision: 1.1 $
	  */
	public class AddCourseCommand implements ICommand, IResponder
	{
	  	public function AddCourseCommand()
		{	 
		}
	
		/**
		  * Set up the command, starting the chain with the delegate call
		  * @param event The event which triggers the call chain
		  */
		public function execute( event : CairngormEvent ): void
		{
			var addCourseEvent:AddCourseEvent = AddCourseEvent( event );
			
			var delegate : AddCourseDelegate = new AddCourseDelegate( this );
			delegate.getCourseComponents(addCourseEvent.courseId, addCourseEvent.parentCourseId, addCourseEvent.title, addCourseEvent.titleUser, addCourseEvent.authorId, addCourseEvent.mapView, addCourseEvent.navigationMap, addCourseEvent.state);		    			
		}
	
		/**
		  * Evaluate the SOAP result
		  * @param event The SOAP server result
		  */
		public function result( event : Object ) : void
		{			
			//set courseId for the selected treeNode
			var model : CourseModelLocator = CourseModelLocator.getInstance();
			var courseNode : CourseVO = model.selectedCourseTreeNode;
			courseNode.id = event.result.CourseID;
			courseNode.isValid = true;	//mark course as valid (will be added to the treeView)
			model.selectedCourseTreeNode = null;		
			model.selectedCourseTreeNode = courseNode;
		}
	
		/**
		  * The fault handler
		  * Report a simple error message to the user
		  * @param The failure event
		  */
		public function fault( event : Object ) : void
		{
			var faultEvent : FaultEvent = FaultEvent( event );
			Alert.show( "Course could not be added!" );
		}
	}

}